package com.manyTomany.both_way.组件类.service;

import com.manyTomany.both_way.组件类.model.Item;
import com.manyTomany.both_way.组件类.model.LineItem;
import com.manyTomany.both_way.组件类.model.Order;
import com.utils.HibernateUtil;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import java.util.Set;

public class BusinessService {

    private HibernateUtil hibernateUtil = new HibernateUtil();
    private SessionFactory sessionFactory = hibernateUtil.getSessionFactory();

    private void saveOrder(Order order) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            session.save(order);
            System.out.println("");
            tx.commit();
        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            throw e;
        } finally {
            session.close();
        }
    }

    private void saveItem(Item item) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            System.out.println();
            session.save(item);
            tx.commit();

        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            throw e;
        } finally {
            session.close();
        }
    }
    private Order loadOrder(Long id) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            Order order = (Order) session.get(Order.class, id);

            Set lineItems = order.getLineItems();
            for (Object lineItem1 : lineItems) {
                LineItem lineItem = (LineItem) lineItem1;
                Hibernate.initialize(lineItem.getItem()); //��ʼ��Item
            }
            tx.commit();

            return order;

        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            throw e;
        } finally {
            session.close();
        }
    }

    private void printOrder(Order order) {
        System.out.println("订单编号:" + order.getOrderNumber());
        System.out.println("总价格:" + order.getPrice());

        Set lineItems = order.getLineItems();
        for (Object lineItem1 : lineItems) {
            LineItem lineItem = (LineItem) lineItem1;
            System.out.println("-----------------------");
            System.out.println("商品名:" + lineItem.getItem().getName());
            System.out.println("购买数量:" + lineItem.getQuantity());
            System.out.println("商品单价:" + lineItem.getBasePrice());
            System.out.println("单元价格:" + lineItem.getUnitPrice());
        }

    }

    private void test() {

        Item item1 = new Item("NEC500", 1000, null);
        Item item2 = new Item("BELL4560", 1800, null);
        saveItem(item1);
        saveItem(item2);

        Order order = new Order();
        order.setOrderNumber("Order001");
        LineItem lineItem1 = new LineItem(item1, order, item1.getBasePrice(), 2);
        LineItem lineItem2 = new LineItem(item2, order, item2.getBasePrice(), 1);

        order.getLineItems().add(lineItem1);
        order.getLineItems().add(lineItem2);
        saveOrder(order);

        order = loadOrder(order.getId());
        printOrder(order);
        sessionFactory.close();

    }

    public static void main(String args[]) {
        new BusinessService().test();
    }
}
